<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 积分商城-商品管理
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;

class ScoreshopProduct extends Common
{
    public function initialize(){
		parent::initialize();
		if(bid > 0) showmsg('无访问权限');
	}
	//商品列表
    public function index(){
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = input('param.field').' '.input('param.order');
			}else{
				$order = 'id desc';
			}
			$where = array();
			$where[] = ['aid','=',aid];
			if(input('param.name')) $where[] = ['name','like','%'.$_GET['name'].'%'];
            if(input('param.cid')) $where[] = ['cid','=',$_GET['cid']];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['status','=',input('param.status')];

			$clist = Db::name('scoreshop_category')->where('aid',aid)->select()->toArray();
			$cdata = array();
			foreach($clist as $c){
				$cdata[$c['id']] = $c['name'];
			}
			$count = 0 + Db::name('scoreshop_product')->where($where)->count();
			$data = Db::name('scoreshop_product')->where($where)->page($page,$limit)->order($order)->select()->toArray();
			foreach($data as $k=>$v){
				$data[$k]['cname'] = $cdata[$v['cid']];
			}
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}
		//分类
		$clist = Db::name('scoreshop_category')->Field('id,name')->where('aid',aid)->where('status',1)->where('pid',0)->order('sort desc,id')->select()->toArray(); 
		foreach($clist as $k=>$v){
			$clist[$k]['child'] = Db::name('scoreshop_category')->Field('id,name')->where('aid',aid)->where('status',1)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray(); 
		}
		View::assign('clist',$clist);
		
		if(session('BST_ID')){
			$userlist = Db::name('admin_user')->field('id,aid,un')->where('id','<>',$this->user['id'])->where('bid',0)->where('isadmin',1)->select()->toArray();
			View::assign('cancopy',true);
		}else{
			$userlist = [];
			View::assign('cancopy',false);
		}
		View::assign('userlist',$userlist);

		return View::fetch();
    }
	//编辑商品
	public function edit(){
		if(input('param.id')){
			$info = Db::name('scoreshop_product')->where('aid',aid)->where('id',input('param.id/d'))->find();
		}
		//分类
		$clist = Db::name('scoreshop_category')->Field('id,name')->where('aid',aid)->where('status',1)->where('pid',0)->order('sort desc,id')->select()->toArray(); 
		foreach($clist as $k=>$v){
			$clist[$k]['child'] = Db::name('scoreshop_category')->Field('id,name')->where('aid',aid)->where('status',1)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray(); 
		}
		$freightList = Db::name('freight')->where('aid',aid)->where('status',1)->order('sort desc,id')->select()->toArray();
		$freightdata = array();
		if($info && $info['freightdata']){
			$freightdata = Db::name('freight')->where('aid',aid)->where('id','in',$info['freightdata'])->order('sort desc,id')->select()->toArray();
		}
		$info['lvprice_data'] = json_decode($info['lvprice_data'], true);

		//分成结算类型
        $sysset = Db::name('admin_set')->where('aid',aid)->find();
        if($sysset['fxjiesuantype'] == 1) {
            $jiesuantypeDesc = '成交价';
        }elseif($sysset['fxjiesuantype'] == 2) {
            $jiesuantypeDesc = '销售利润';
        } else {
            $jiesuantypeDesc = '销售价';
        }

        $default_cid = Db::name('member_level_category')->where('aid',aid)->where('isdefault', 1)->value('id');
        $default_cid = $default_cid ? $default_cid : 0;
        $aglevellist = Db::name('member_level')->where('aid',aid)->where('cid', $default_cid)->where('can_agent','<>',0)->order('sort,id')->select()->toArray();
        $levellist = Db::name('member_level')->where('aid',aid)->where('cid', $default_cid)->order('sort,id')->select()->toArray();
        View::assign('aglevellist',$aglevellist);
        View::assign('levellist',$levellist);
		View::assign('freightList',$freightList);
		View::assign('freightdata',$freightdata);
		View::assign('clist',$clist);
		View::assign('info',$info);
        View::assign('jiesuantypeDesc',$jiesuantypeDesc);
		return View::fetch();
	}
	//保存商品
	public function save(){
		if(input('post.id')) $product = Db::name('scoreshop_product')->where('aid',aid)->where('id',input('post.id/d'))->find();
		$info = input('post.info/a');

		$info['detail'] = \app\common\Common::geteditorcontent($info['detail']);
		$data = array();
		$data['cid'] = $info['cid'];
		$data['name'] = $info['name'];
		$data['pic'] = $info['pic'];
		$data['pics'] = $info['pics'];
		//$data['fuwupoint'] = $info['fuwupoint'];
		//$data['sellpoint'] = $info['sellpoint'];
		//$data['procode'] = $info['procode'];
		$data['sell_price'] = $info['sell_price'];
        $data['cost_price'] = $info['cost_price'];
        $data['score_price'] = $info['score_price'];
        $data['money_price'] = $info['money_price'];
		$data['weight'] = $info['weight'];
		$data['stock'] = $info['stock'];
		if($info['oldsales'] != $info['sales']){
			$data['sales'] = $info['sales'];
		}
		$data['sort'] = $info['sort'];
		$data['buymax'] = $info['buymax'];
		$data['status'] = $info['status'];
		$data['detail'] = $info['detail'];
		$data['freighttype'] = $info['freighttype'];
		$data['freightdata'] = $info['freightdata'];
		$data['freightcontent'] = $info['freightcontent'];
        $data['lvprice'] = $info['lvprice'];

        $data['commissionset'] = $info['commissionset'];
        $data['commissiondata1'] = jsonEncode(input('post.commissiondata1/a'));
        $data['commissiondata2'] = jsonEncode(input('post.commissiondata2/a'));
        $data['commissiondata3'] = jsonEncode(input('post.commissiondata3/a'));
        $data['commissiondata5'] = jsonEncode(input('post.commissiondata5/a'));
        if($info['lvprice']==1){
            $data['lvprice_data'] = jsonEncode($info['lvprice_data']);

            $default_cid = Db::name('member_level_category')->where('aid',aid)->where('isdefault', 1)->value('id');
            $default_cid = $default_cid ? $default_cid : 0;
            $levellist = Db::name('member_level')->where('aid',aid)->where('cid', $default_cid)->order('sort,id')->select()->toArray();
            $defaultlvid = $levellist[0]['id'];
            $money_price = 0;$score_price = 0;$lvprice_data = [];
            foreach($info['lvprice_data'] as $ks=>$v){
                if($money_price==0 && $ks == $defaultlvid){
                    $money_price = $v['money_price'];
                    $score_price = $v['score_price'];
                }
            }

            $data['score_price'] = $score_price;
            $data['money_price'] = $money_price;
        }

		if(!$product) $data['createtime'] = time();
        if($product){
			Db::name('scoreshop_product')->where('aid',aid)->where('id',$product['id'])->update($data);
			$proid = $product['id'];
			\app\common\System::plog('编辑积分商城商品'.$product['id']);
		}else{
			$data['aid'] = aid;
			$proid = Db::name('scoreshop_product')->insertGetId($data);
			\app\common\System::plog('添加积分商城商品'.$proid);
		}
		return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('index')]);
	}
	//改状态
	public function setst(){
		$st = input('post.st/d');
		$ids = input('post.ids/a');
		Db::name('scoreshop_product')->where('aid',aid)->where('id','in',$ids)->update(['status'=>$st]);
		\app\common\System::plog('积分商城商品改状态'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//删除
	public function del(){
		$ids = input('post.ids/a');
		Db::name('scoreshop_product')->where('aid',aid)->where('id','in',$ids)->delete();
		\app\common\System::plog('积分商城商品删除'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}
	//选择商品
	public function chooseproduct(){
		//分类
		$clist = Db::name('scoreshop_category')->Field('id,name')->where('aid',aid)->where('status',1)->where('pid',0)->order('sort desc,id')->select()->toArray(); 
		foreach($clist as $k=>$v){
			$clist[$k]['child'] = Db::name('scoreshop_category')->Field('id,name')->where('aid',aid)->where('status',1)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray(); 
		}
		View::assign('clist',$clist);
		return View::fetch();
	}
	//获取商品信息
	public function getproduct(){
		$proid = input('post.proid/d');
		$product = Db::name('scoreshop_product')->where('aid',aid)->where('id',$proid)->find();
		return json(['product'=>$product]);
	}

	//获取分类信息
	public function getcategory(){
		if(!session('BST_ID')) return json(['status'=>0,'msg'=>'无权限操作']);
		$toaid = input('param.toaid/d');
		//分类
		$clist = Db::name('scoreshop_category')->Field('id,name')->where('aid',$toaid)->where('pid',0)->order('sort desc,id')->select()->toArray(); 
		foreach($clist as $k=>$v){
			$child = Db::name('scoreshop_category')->Field('id,name')->where('aid',$toaid)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray();
			$clist[$k]['child'] = $child;
		}
		return json(['status'=>1,'data'=>$clist]);
	}
	//复制到其他账号商品
	public function userProcopy(){
		if(!session('BST_ID')) return json(['status'=>0,'msg'=>'无权限操作']);
		$ids = input('post.ids/a');
		$toaid = input('param.toaid');
		$tocid = input('param.tocid');
		if(!$toaid) return json(['status'=>0,'msg'=>'请选择账号']);
		//if(!$tocid) return json(['status'=>0,'msg'=>'请选择分类']);
		if(!$tocid) $tocid = 0;
		if(!$ids) $ids = array(input('post.id/d'));
		$where = [];
		$where[] = ['aid','=',aid];
		//$where[] = ['bid','=',bid];
		$where[] = ['id','in',$ids];
		$prolist = Db::name('scoreshop_product')->where($where)->select()->toArray();
		if(!$prolist) return json(['status'=>0,'msg'=>'商品不存在,请重新选择']);

		foreach($prolist as $product){
			$data = $product;
			$data['aid'] = $toaid;
			$data['name'] = $data['name'];
			$data['status'] = 0;
			$data['cid'] = $tocid;
			unset($data['id']);
			$newproid = Db::name('scoreshop_product')->insertGetId($data);
		}
		return json(['status'=>1,'msg'=>'复制成功']);
	}
}
